package com.example.myapplication;

import android.app.Service;
import android.content.Intent;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;

import java.io.IOException;
import java.util.List;

public class FloatingVideoService extends Service {

    private WindowManager windowManager;
    private WindowManager.LayoutParams layoutParams;
    private View displayView;
    private SurfaceView surface_view;
    private Camera mCamera;

    @Override
    public void onCreate() {
        super.onCreate();
        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        layoutParams = new WindowManager.LayoutParams();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;
        }
        layoutParams.format = PixelFormat.RGBA_8888;
        layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        layoutParams.width = 300;
        layoutParams.height = 300;
        layoutParams.x = 300;
        layoutParams.y = 300;
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        showFloatingWindow();

        return new MyBinder();
    }

    public class MyBinder extends Binder {

        public void addView() {
            windowManager.addView(displayView,layoutParams);
        }

        public void start() {
            startCamera();
        }

        public void close() {
            release();
        }

        public void test1() {

        }

        public void test2() {

        }

    }


    private void showFloatingWindow() {
        if (Settings.canDrawOverlays(this)) {
            LayoutInflater layoutInflater = LayoutInflater.from(this);
            displayView = layoutInflater.inflate(R.layout.video_display, null);
            displayView.setOnTouchListener(new FloatingOnTouchListener());
            surface_view = displayView.findViewById(R.id.surface_view);
            windowManager.addView(displayView, layoutParams);
        }

    }

    private void startCamera() {
        mCamera = Camera.open(1);
        //配置camera的属性
        Camera.Parameters parameters = mCamera.getParameters();
//        List<Camera.Size> list = parameters.getSupportedPreviewSizes();
//        for (int i = 0; i < list.size(); i++) {
//            Log.e("lin", "----- size=" + list.get(i).width+"    "+list.get(i).height);
//        }
        // 设置摄像头 图像传感器的角度、方向
        mCamera.setDisplayOrientation(90);
        // 设置照片的大小
//        parameters.setPictureSize(720, 720);
        parameters.setPreviewSize(720, 720);
        //配置数据回调的格式
        parameters.setPreviewFormat(ImageFormat.YV12);
        // 设置自动对焦
        mCamera.setParameters(parameters);
        //设置预览画面
        try {
            mCamera.setPreviewDisplay(surface_view.getHolder());
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    private void release() {
        mCamera.stopPreview();
        mCamera.lock();
        mCamera.release();
        mCamera = null;
        windowManager.removeView(displayView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        release();
    }

    private class FloatingOnTouchListener implements View.OnTouchListener {
        private int x;
        private int y;

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    x = (int) event.getRawX();
                    y = (int) event.getRawY();
                    break;
                case MotionEvent.ACTION_MOVE:
                    int nowX = (int) event.getRawX();
                    int nowY = (int) event.getRawY();
                    int movedX = nowX - x;
                    int movedY = nowY - y;
                    x = nowX;
                    y = nowY;
                    layoutParams.x = layoutParams.x + movedX;
                    layoutParams.y = layoutParams.y + movedY;
                    windowManager.updateViewLayout(view, layoutParams);
                    break;
                default:
                    break;
            }
            return true;
        }
    }

}
